package com.xiaoke.service.impl;

import com.xiaoke.service.CommonService;
import com.xiaoke.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 公共服务实现类
 *
 * @author xiaoke
 * @since 2023-03-24 20:16:37
 */
@Service("commonService")
public class CommonServiceImpl implements CommonService {
    @Resource
    private HttpServletRequest request;

    @Override
    public Integer getUserId() {
        String token = request.getHeader("Authorization");
        String userId = null;
        try {
            Claims claims = JwtUtil.parseJWT(token);
            userId = claims.getSubject();
        } catch (Exception ignored) {

        }
        assert userId != null;
        return Integer.valueOf(userId);
    }
}
